/*
5. 编程题

        使用集合实现斗地主游戏的部分功能，要求如下：

        （1）首先准备 54 张扑克牌并打乱顺序。

        （2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。

        （3）查看三个玩家手中的扑克牌和底牌。

        （4）其中玩家手中的扑克牌需要按照大小顺序打印，规则如下：

        手中扑克牌从大到小的摆放顺序：大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
*/


import java.util.*;

public class Q5 {
    public static class Card {
        private final String value;
        public static String[] values = new String[]{"大王", "小王", "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
        private static final ArrayList<Card> CARDS = generateSet();

        private Card(String value) {
            this.value = value;
        }

        private static ArrayList<Card> generateSet() {
            ArrayList<Card> Cards = new ArrayList<>();
            Cards.add(new Card(values[0]));
            Cards.add(new Card(values[1]));
            for (int idx = 2; idx < values.length; idx++) {
                for (int i = 0; i < 4; i++) {
                    Cards.add(new Card(values[idx]));
                }
            }
            return Cards;
        }

        public String getValue() {
            return value;
        }

        @Override
        public String toString() {
            return value;
        }
    }


    private static void printPlayerCards(List<Card> player) {
        System.out.print("排序前");
        System.out.println(player);
        Collections.sort(player,
                new Comparator<Card>() {
                    @Override
                    public int compare(Card o1, Card o2) {
                        List<String> tmp = Arrays.asList(Card.values);
                        return tmp.indexOf(o1.getValue()) - tmp.indexOf(o2.getValue());
                    }
                });
        System.out.print("排序后");
        System.out.println(player);
    }

    public static void main(String[] args) {
        //洗牌前
        //ArrayList<Card> cards = Card.generateSet();
        ArrayList<Card> cards = Card.CARDS;
        //Collections.shuffle(Card.CARDS);
        //System.out.println(Card.CARDS);
        System.out.println("洗牌前");
        System.out.println(cards);
        //牌的数量
        System.out.println("牌的数量");
        System.out.println(cards.size());
        //洗牌后
        Collections.shuffle(cards);
        System.out.println("洗牌后牌的情况");
        System.out.println(cards);

        //打印三个牌手的手牌
        System.out.println("牌手1");
        printPlayerCards(cards.subList(0, 18));
        System.out.println("牌手2");
        printPlayerCards(cards.subList(18, 36));
        System.out.println("牌手3");
        printPlayerCards(cards.subList(36, 54));
    }
}
